package com.lzh.dlykserver.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzh.dlykserver.DlykServerApplication;
import com.lzh.dlykserver.constant.Constants;
import com.lzh.dlykserver.entity.TActivity;
import com.lzh.dlykserver.mapper.TActivityMapper;
import com.lzh.dlykserver.query.ActivityQuery;
import com.lzh.dlykserver.result.DicEnum;
import com.lzh.dlykserver.service.ActivityService;
import com.lzh.dlykserver.util.JWTUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class ActivityServiceImpl implements ActivityService {

    @Resource
    private TActivityMapper tActivityMapper;

    @Override
    public PageInfo<TActivity> getactivityByPage(Integer current, ActivityQuery activityQuery) {
        //设置PageHelper
        PageHelper.startPage(current, Constants.PAGE_SIZE);
        //查询
        List<TActivity> list = tActivityMapper.selectUserByPage(activityQuery);
        //封装分页数据到PageInfo
        PageInfo<TActivity> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int saveActivity(ActivityQuery activityQuery) {
        TActivity activity = new TActivity();

        //把activityQuery中的属性复制到tActivity中(复制要求，两个对象的属性名相同，类型相同)
        BeanUtils.copyProperties(activityQuery, activity);

        //创建时间
        activity.setCreateTime(new Date());

        //创建人id
        Integer loginUserId = JWTUtils.parseUserFromJWT(activityQuery.getToken()).getId();
        activity.setCreateBy(loginUserId);

        return tActivityMapper.insertSelective(activity);
    }

    @Override
    public TActivity getActivityById(Integer id) {
        return tActivityMapper.selectDetailByPrimaryKey(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateActivity(ActivityQuery activityQuery) {
        TActivity tActivity = new TActivity();

        //把activityQuery中的属性复制到tActivity中(复制要求，两个对象的属性名相同，类型相同)
        BeanUtils.copyProperties(activityQuery, tActivity);

        //编辑时间
        tActivity.setEditTime(new Date());
        //编辑人id
        Integer editUserId = JWTUtils.parseUserFromJWT(activityQuery.getToken()).getId();
        tActivity.setCreateBy(editUserId);

        return tActivityMapper.updateByPrimaryKeySelective(tActivity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteActivity(Integer id) {
        return tActivityMapper.deleteByPrimaryKey(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int batchDeleteActivity(List<String> idList) {
        return tActivityMapper.delByIds(idList);
    }

    @Override
    public List<TActivity> getAllOnGoingActivity() {
        // 强制刷新缓存
        DlykServerApplication.cacheMap.remove(DicEnum.ACTIVITY.getCode());
        List<TActivity> tActivityList = tActivityMapper.selectAllOnGoingActivity();
        DlykServerApplication.cacheMap.put(DicEnum.ACTIVITY.getCode(), tActivityList);
        return tActivityList;
    }
}
